extends Node
class_name SynergyManager

signal synergy_activated(synergy_name, level)
signal synergies_updated(active_synergies)

var synergy_definitions = {
	"Tank": {
		"thresholds": [2, 4],
		"effects": [
			{"type": "armor", "value": 15, "target": "class"},
			{"type": "armor", "value": 30, "target": "class"}
		],
		"description": ["2: +15 Armor", "4: +30 Armor"]
	},
	"Fighter": {
		"thresholds": [2, 4, 6],
		"effects": [
			{"type": "attack_damage", "value": 10, "target": "class"},
			{"type": "attack_damage", "value": 20, "target": "class"},
			{"type": "attack_damage", "value": 35, "target": "class"}
		],
		"description": ["2: +10 AD", "4: +20 AD", "6: +35 AD"]
	},
	"Ranged": {
		"thresholds": [2, 3],
		"effects": [
			{"type": "attack_speed", "value": 0.25, "target": "class"},
			{"type": "attack_speed", "value": 0.5, "target": "class"}
		],
		"description": ["2: +25% AS", "3: +50% AS"]
	},
	"Mage": {
		"thresholds": [2, 3, 4],
		"effects": [
			{"type": "ability_power", "value": 20, "target": "class"},
			{"type": "ability_power", "value": 40, "target": "class"},
			{"type": "ability_power", "value": 75, "target": "class"}
		],
		"description": ["2: +20 AP", "3: +40 AP", "4: +75 AP"]
	},
	"Assassin": {
		"thresholds": [2, 4],
		"effects": [
			{"type": "crit_chance", "value": 25, "target": "class"},
			{"type": "crit_chance", "value": 50, "target": "class"}
		],
		"description": ["2: +25% Crit", "4: +50% Crit"]
	}
}

var active_synergies = {}
var unit_counts = {}

func check_synergies(units: Array):
	unit_counts.clear()
	active_synergies.clear()
	
	# Count units by class
	for unit in units:
		if unit.has_method("get_unit_class"):
			var unit_class = unit.get_unit_class()
			if unit_class in unit_counts:
				unit_counts[unit_class] += 1
			else:
				unit_counts[unit_class] = 1
	
	# Check which synergies are active
	for synergy_name in synergy_definitions:
		if synergy_name in unit_counts:
			var count = unit_counts[synergy_name]
			var definition = synergy_definitions[synergy_name]
			
			for i in range(definition.thresholds.size() - 1, -1, -1):
				if count >= definition.thresholds[i]:
					active_synergies[synergy_name] = {
						"level": i,
						"count": count,
						"effect": definition.effects[i],
						"description": definition.description[i]
					}
					emit_signal("synergy_activated", synergy_name, i)
					break
	
	emit_signal("synergies_updated", active_synergies)
	apply_synergy_effects(units)

func apply_synergy_effects(units: Array):
	# Reset all unit stats to base first
	for unit in units:
		if unit.has_method("reset_to_base_stats"):
			unit.reset_to_base_stats()
	
	# Apply active synergy effects
	for synergy_name in active_synergies:
		var synergy = active_synergies[synergy_name]
		var effect = synergy.effect
		
		for unit in units:
			if unit.has_method("get_unit_class") and unit.get_unit_class() == synergy_name:
				apply_effect_to_unit(unit, effect)

func apply_effect_to_unit(unit, effect):
	match effect.type:
		"armor":
			if unit.has_method("add_armor"):
				unit.add_armor(effect.value)
		"attack_damage":
			if unit.has_method("add_attack_damage"):
				unit.add_attack_damage(effect.value)
		"attack_speed":
			if unit.has_method("add_attack_speed"):
				unit.add_attack_speed(effect.value)
		"ability_power":
			if unit.has_method("add_ability_power"):
				unit.add_ability_power(effect.value)
		"crit_chance":
			if unit.has_method("add_crit_chance"):
				unit.add_crit_chance(effect.value)

func get_synergy_info() -> Dictionary:
	return {
		"active": active_synergies,
		"counts": unit_counts,
		"definitions": synergy_definitions
	}

func get_active_synergy_descriptions() -> Array:
	var descriptions = []
	for synergy_name in active_synergies:
		var synergy = active_synergies[synergy_name]
		descriptions.append({
			"name": synergy_name,
			"count": synergy.count,
			"description": synergy.description
		})
	return descriptions